import logging
import os
import shutil
import time

import facade
import pymysql
from bs4 import BeautifulSoup
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "cell", "download", "issue")
sPath2 = BaseDir.get_new_path(TopPath, "download", "cell", "download", "issue2")
BaseDir.create_dir(sPath2)
mysqlutils = facade.MysqlUtiles(BaseDir.get_new_path(curPath, "db.ini"), "db", facade.get_streamlogger())

nCount = 0
ListSqls = []


# # 数据库链接
# def MajorDbConnect():
#     return MySqlDbConnect(curPath, "db.ini")


def GetFileName(filePath):
    filepathlist = filePath.split(os.sep)
    filename = filepathlist.pop()
    filename = filename.split(os.extsep)[0]
    filename = filename.split("_")[1]
    soup = BeautifulSoup(open(filePath, 'r', encoding='utf-8'), "lxml")
    div_tag_list = soup.find_all('li', class_="toc__item clearfix")
    num = len(div_tag_list)
    for div in div_tag_list:
        a_title = div.find("a")
        href = a_title["href"]
        nameall = "".join(a_title.stripped_strings)
        if nameall == "":
            try:
                nameall = a_title["title"]
            except:
                nameall = ""
        nameall = pymysql.escape_string(nameall)
        rawid = href.split("/")[-1].strip()
        sql = '''INSERT IGNORE INTO archive (`rawid`,`issueppi`, `title`, `url`, `stat`,`num`) VALUES("{rawid}","{name}", "{title}","{url}",{stat},{num}) '''.format(
            name=filename, title=nameall, url=href, stat=0, num=num, rawid=rawid)

        # sql = '''insert into `archive` (`rawid`,`issueppi`, `title`, `url`, `stat`,`num`) values("{rawid}","{name}", "{title}","{url}",{stat},{num}) on DUPLICATE key update `issueppi`="{name}",`num`={num}'''.format(
        #     name=filename, title=nameall, url=href, stat=0, num=num, rawid=rawid)
        ListSqls.append(sql)
    print("sql list长度为：" + str(len(ListSqls)))
    if len(ListSqls) > 10000:
        InsertIntoDbFromList()
        time.sleep(10)


# 插入数据库
def InsertIntoDbFromList():
    global ListSqls
    # 数据库连接
    # conn = MajorDbConnect()
    #     # ExeSqlList(ListSqls, conn)
    mysqlutils.ExeSqlListToDB(ListSqls)
    ListSqls = list()


def main(logger1: logging.Logger = None):
    global logger
    logger = logger1
    filenum = 0
    for filePath in BaseDir.get_dir_all_files(sPath):
        print(filePath)
        GetFileName(filePath)
        soucpath = BaseDir.get_new_path(sPath2, BaseDir.get_file_name(filePath))
        shutil.move(filePath, soucpath)
        filenum += 1
        print("完成解析文件数量{}".format(filenum))
    print(len(ListSqls))
    InsertIntoDbFromList()


if __name__ == "__main__":
    main()
